package com.xxl.sso.server.util;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @ClassName: CaptchaUtils
 * @Description: 图片验证码工具类
 * @Author: wugongzi
 * @Date: 2021/12/7 9:37
 * @Version: 1.0
 */
@Getter
@Setter
@Slf4j
public class CaptchaUtils {

    private static Random random = new Random();

    /**
     * 随机产生数字与字母组合的字符串
     */
    private static final String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 图片宽
     */
    private static final int width = 80;

    /**
     * 图片高
     */
    private static final int height = 26;

    /**
     * 字体高度
     */
    private static final int fontHeight = 18;

    /**
     * 干扰线数量
     */
    private static final int lineSize = 40;

    /**
     * 随机产生字符数量
     */
    private static final int num = 4;

    /**
     * 生成随机图片
     */
    public static CaptchaDTO create() {
        // BufferedImage类(画板)
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        // Graphics类(画纸),该对象可以在图像上进行各种绘制操作
        Graphics g = image.getGraphics();
        g.fillRect(0, 0, width, height);
        // g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
        g.setColor(Color.WHITE);
        // 绘制随机字符
        String captchaCode = "";
        for (int i = 1; i <= num; i++) {
            captchaCode = drawString(g, captchaCode, i);
        }
        // 释放图形上下文
        g.dispose();
        // 构建返回数据
        CaptchaDTO captchaDTO = new CaptchaDTO();
        captchaDTO.setCaptchaCode(captchaCode);
        captchaDTO.setBufferedImage(image);
        return captchaDTO;
    }

    @Data
    public static class CaptchaDTO {
        /**
         * 验证码
         */
        private String captchaCode;

        private BufferedImage bufferedImage;
    }

    /*
     * 获取随机颜色
     */
    private static Color getRandomColor(int r, int g, int b) {
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        r = random.nextInt(r);
        g = random.nextInt(g);
        b = random.nextInt(b);
        return new Color(r, g, b);
    }

    /*
     * 绘制字符串
     */
    private static String drawString(Graphics g, String captchaCode, int i) {
        // 设置字体，字体的大小应该根据图片的高度来定。
        g.setFont(new Font("Times New Roman", Font.CENTER_BASELINE, fontHeight));
        g.setColor(getRandomColor(101,111,121));
        String randomString = getRandomString(random.nextInt(randString.length()));
        captchaCode += randomString;
        g.translate(random.nextInt(3), random.nextInt(3));
        g.drawString(randomString, 13 * i, 16);
        return captchaCode;
    }

    /*
     * 绘制干扰线
     */
    private static void drawLine(Graphics g) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(13);
        int yl = random.nextInt(15);
        g.drawLine(x, y, x + xl, y + yl);
    }

    /*
     * 获取随机的字符
     */
    private static String getRandomString(int num) {
        return String.valueOf(randString.charAt(num));
    }

}